﻿using System;
using System.Diagnostics;

namespace HS.Utility
{
    class Timing
    {
        TimeSpan start; //开始
        TimeSpan duration; //持续

        public Timing()
        {
            start = new TimeSpan(0);
            duration = new TimeSpan(0);
        }

        public void StartTime()
        {
            GC.Collect(); //强制执行垃圾回收
            GC.WaitForPendingFinalizers(); //等待垃圾回收完毕
            start = Process.GetCurrentProcess().Threads[0].UserProcessorTime; //获取当前线程的当前时间
        }

        public void StopTime()
        {
            duration = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(start); //或去当前线程的执行时间
        }

        //获得持续时间
        public TimeSpan GetDuration()
        {
            return duration;
        }
    }
}
